﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TransFormer.BLL;

namespace TransFormer.Web.admin
{
    public partial class ArticleEdit : AdminPage
    {
        private string _oldImgPath;
        private BLL.Article _bll = new Article();
        private log4net.ILog logger = log4net.LogManager.GetLogger(typeof(ArticleEdit));
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            int id = -1;
            if (int.TryParse(Request["Id"], out id))
            {
                var model = _bll.GetModel(id);
                txtId.Text = model.ID.ToString();
                txtTitle.Text = model.Title;
                txtContent.Text = model.ArticleContent;

                Image1.ImageUrl = "." + model.PicUrl;
                Image1.DataBind();
                language.Items.FindByValue(model.Language.Trim()).Selected = true;
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            Model.Article article = new Model.Article();

            try
            {
                if (fileImg.HasFile)
                {
                    if (fileImg.FileBytes.Length > 1000000)
                    {
                        lbResult.InnerHtml = "您上传参数文件过大，请减小文件体积后重新上传！";
                        return;
                    }
                    string productPath = Server.MapPath("../images/Article/");
                    string name = Path.GetFileNameWithoutExtension(fileImg.FileName) + Guid.NewGuid() + ".jpg";
                    productPath += name;
                    fileImg.SaveAs(productPath);

                    var model = _bll.GetModel(int.Parse(txtId.Text));
                    if (!string.IsNullOrEmpty(model.PicUrl))
                    {
                        string path = Server.MapPath("." + model.PicUrl);
                        try
                        {
                            File.Delete(path);
                        }
                        catch (Exception exception)
                        {
                            logger.Error(exception.Message, exception);
                        }
                    }
                    article.PicUrl = "./images/Article/" + name;
                }
                article.ID = int.Parse(txtId.Text);
                article.Title = txtTitle.Text;
                article.ArticleContent = txtContent.Text;
                article.Date = DateTime.Now;
                article.Language = language.Value;
                article.People = Session["userName"] == null ? string.Empty : Session["userName"].ToString();

                if (_bll.Update(article))
                {
                    Response.Redirect("articlelist.aspx", false);
                }
                else
                {
                    lbResult.InnerHtml = "添加产品错误！！！请联系管理员";

                }
            }
            catch (Exception exception)
            {
                logger.Error(exception.Message, exception);
                Response.Redirect("../error.html");
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("articlelist.aspx", false);
        }
    }
}